package com.situ.stmall.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.situ.stmall.entity.APIResult;
import com.situ.stmall.entity.Root;
import com.situ.stmall.service.RootService;

@RestController
@RequestMapping("/api/roots")
@CrossOrigin(origins = {"*"})
public class RootController {

	@Autowired
	private RootService rootService;
	/**
	 * 登陆-查询
	 */
	@GetMapping
	public Object login(Root root, String vercode, 
			HttpSession session) {
		//1.获取参数
		//判断vercode是否正确
		
		//2.调用service层
		APIResult apiResult = new APIResult();
		try {
			Root loginRoot = rootService.login(root);
			//清除密码数据
			loginRoot.setPassword(null);
			
			//登陆成功
			//将用户信息保存进入session中
			session.setAttribute("root", loginRoot);
			//返回数据
			apiResult.setStatus(200);
			apiResult.setData(loginRoot);
			
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			//登陆失败
			//返回数据
			apiResult.setStatus(404);
			apiResult.setMsg(e.getMessage());
		}
		
		//3.返回数据
		return apiResult;
	}
}
